package run.siyuan.manag.controller.admin;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import run.siyuan.common.entity.Result;
import run.siyuan.common.exception.ExceptionProperties;
import run.siyuan.manag.entity.SysConfig;
import run.siyuan.manag.service.SysConfigService;

import java.util.List;

/**
 * <p>
 * 系统配置表 前端控制器
 * </p>
 *
 * @author siyuan
 * @since 2021-01-13
 */
@Api(tags = "系统配置管理")
@RestController
@RequestMapping("/manag/sysConfig")
public class SysConfigController {

    @Autowired
    private SysConfigService configService;
    @Autowired
    private ExceptionProperties properties;


    @ApiOperation("新增或修改")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SysConfig sysConfig) {
        boolean result = false;
        if (StrUtil.isBlank(sysConfig.getId())) {
            result = configService.save(sysConfig);
        } else {
            result = configService.updateById(sysConfig);
        }
        if (!result) {
            return Result.error(50000, properties.getCodeName("50000"));
        }
        return Result.success();
    }

    @ApiOperation(value = "详情", response = SysConfig.class)
    @GetMapping("/getOne/{id}")
    public Result getDictById(@PathVariable Long id) {
        if (ObjectUtil.isEmpty(id)) {
            return Result.error(50001, properties.getCodeName("50001"));
        }
        return Result.success().data(configService.getById(id));
    }

    @ApiOperation(value = "列表", response = SysConfig.class)
    @GetMapping("/list")
    public Result list() {
        return Result.success().data(configService.list());
    }

    @ApiOperation(value = "分页", notes = "分页", response = SysConfig.class)
    @GetMapping("/page/{page}/{limit}")
    public Result page(@ApiParam(value = "当前页码", required = true, defaultValue = "1") @PathVariable Long page,
                       @ApiParam(value = "每页记录数", required = true, defaultValue = "10") @PathVariable Long limit) {
        try {
            Page<SysConfig> requestPage = new Page<>(page, limit);
            return new Result().data(configService.page(requestPage,null));
        } catch (Exception e) {
            return new Result<>().data(e.getMessage());
        }
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<String> idList) {
        if (!configService.removeByIds(idList)) {
            return Result.error(50001, properties.getCodeName("50001"));
        }
        return Result.success();
    }

    // TODO 根绝标签查询

}

